<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.bookmanager.model.Category" %>
<%@ page import="com.bookmanager.model.User" %>
<%
    // 检查管理员权限
    User user = (User) session.getAttribute("user");
    if (user == null || !"admin".equals(user.getRole())) {
        response.sendRedirect("../login.jsp");
        return;
    }

    Category category = (Category) request.getAttribute("category");
    if (category == null) {
        response.sendRedirect("book-categories?message=category_not_found");
        return;
    }
%>
<html>
<head>
    <title>编辑分类 - 图书管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f5f7fa;
            color: #333;
        }

        .header {
            background: white;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            padding: 0 20px;
        }

        .nav-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            max-width: 1200px;
            margin: 0 auto;
            height: 70px;
        }

        .logo {
            font-size: 1.5rem;
            font-weight: bold;
            color: #667eea;
        }

        .admin-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .admin-badge {
            background: #28a745;
            color: white;
            padding: 4px 8px;
            border-radius: 12px;
            font-size: 0.8rem;
            font-weight: 500;
        }

        .logout-btn {
            padding: 8px 16px;
            background: #dc3545;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            font-size: 0.9rem;
        }

        .container {
            max-width: 600px;
            margin: 30px auto;
            padding: 0 20px;
        }

        .page-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 2px solid #eee;
        }

        .page-title {
            font-size: 1.8rem;
            color: #333;
        }

        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 8px;
            transition: all 0.3s ease;
        }

        .btn-primary {
            background: #667eea;
            color: white;
        }

        .btn-success {
            background: #28a745;
            color: white;
        }

        .btn-warning {
            background: #ffc107;
            color: #212529;
        }

        .btn-danger {
            background: #dc3545;
            color: white;
        }

        .btn:hover {
            opacity: 0.9;
            transform: translateY(-1px);
        }

        .edit-form {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #333;
        }

        .form-control {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
            box-sizing: border-box;
        }

        .form-control:focus {
            border-color: #667eea;
            outline: none;
            box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
        }

        .form-actions {
            display: flex;
            gap: 10px;
            margin-top: 25px;
            padding-top: 15px;
            border-top: 1px solid #eee;
        }

        .alert {
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 6px;
            font-size: 14px;
        }

        .alert-success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .alert-danger {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
    </style>
</head>
<body>
<!-- 顶部导航 -->
<div class="header">
    <div class="nav-bar">
        <div class="logo">📚 图书管理系统</div>
        <div class="admin-info">
            <span>欢迎，<strong><%= user.getUsername() %></strong></span>
            <span class="admin-badge">管理员</span>
            <a href="<%= request.getContextPath() %>/logout" class="logout-btn">退出登录</a>
        </div>
    </div>
</div>

<div class="container">
    <!-- 页面头部 -->
    <div class="page-header">
        <h1 class="page-title">✏️ 编辑分类</h1>
        <a href="book-categories" class="btn btn-primary">返回分类列表</a>
    </div>

    <!-- 消息提示 -->
    <%
        String message = request.getParameter("message");
        if (message != null) {
            String alertClass = "";
            String alertText = "";

            if ("update_success".equals(message)) {
                alertClass = "alert-success";
                alertText = "分类更新成功！";
            } else if ("update_failed".equals(message)) {
                alertClass = "alert-danger";
                alertText = "分类更新失败！";
            }

            if (!alertText.isEmpty()) {
    %>
    <div class="alert <%= alertClass %>">
        <%= alertText %>
    </div>
    <%      }
    }
    %>

    <!-- 编辑表单 -->
    <div class="edit-form">
        <form action="book-categories" method="post">
            <input type="hidden" name="action" value="update">
            <input type="hidden" name="categoryId" value="<%= category.getCategoryId() %>">

            <div class="form-group">
                <label class="form-label">分类名称 *</label>
                <input type="text" name="categoryName" class="form-control" required
                       value="<%= category.getCategoryName() != null ? category.getCategoryName() : "" %>"
                       placeholder="请输入分类名称">
            </div>

            <div class="form-group">
                <label class="form-label">分类描述</label>
                <textarea name="description" class="form-control" rows="4"
                          placeholder="请输入分类描述"><%= category.getDescription() != null ? category.getDescription() : "" %></textarea>
            </div>

            <div class="form-group">
                <label class="form-label">分类信息</label>
                <div style="background: #f8f9fa; padding: 15px; border-radius: 5px; font-size: 0.9rem;">
                    <div>分类ID: <strong><%= category.getCategoryId() %></strong></div>
                    <div>创建时间: <strong><%= category.getCreateTime() != null ? category.getCreateTime().toLocalDate().toString() : "未知" %></strong></div>
                    <div>图书数量: <strong><%= category.getBookCount() != null ? category.getBookCount() : 0 %> 本</strong></div>
                </div>
            </div>

            <div class="form-actions">
                <button type="submit" class="btn btn-success" style="flex: 1;">更新分类</button>
                <a href="book-categories" class="btn btn-primary">取消</a>
            </div>
        </form>
    </div>
</div>

<script>
    // 表单提交验证
    document.querySelector('form').addEventListener('submit', function(e) {
        const categoryName = this.querySelector('input[name="categoryName"]').value;

        if (!categoryName.trim()) {
            e.preventDefault();
            alert('请填写分类名称！');
            return;
        }
    });
</script>
</body>
</html>
